package com.yang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.exception.BusinessException;
import com.yang.pojo.Archives;
import com.yang.pojo.UserInfo;
import com.yang.service.ArchivesService;
import com.yang.service.UserInfoService;
import com.yang.util.JsonResult;
import com.yang.util.NumberGeneration;
import org.apache.commons.collections4.Get;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2021/3/12 9:05
 * @Version 1.0
 */

@Transactional
@RestController
@RequestMapping("/archives")
public class ArchivesController {

    @Resource
    ArchivesService archivesService;
    @Resource
    UserInfoService userInfoService;

    /****
     * 根据用户id查找用户详细资料
     * @param id
     * @return
     */
    @GetMapping("/getArchivesByUserId")
    public Map<String,Object> getArchivesByUserId(@RequestParam("id") Integer id){
        JsonResult jsonResult = new JsonResult();
        Archives archivesByUserId = archivesService.getArchivesByUserId(id);
        jsonResult.setCode("200");
        jsonResult.setData(archivesByUserId);
        return jsonResult.getValues();
    }

    /***
     * 修改或新增用户详细资料（调用该接口前应存在用户的基本资料）
     * @param archives
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Map<String,Object> saveOrUpdate(@RequestBody Archives archives){
        JsonResult jsonResult = new JsonResult();
        if(archives.getId() == null){//新增
            NumberGeneration numberGeneration = new NumberGeneration();
            UserInfo byId = userInfoService.findById(archives.getUserId());
            archives.setJobNumber(numberGeneration.jobNumber(byId.getDepartmentId(),byId.getPhoneNumber()));
            int sava = archivesService.sava(archives);
            if(sava > 0){
                jsonResult.setCode("200");
                jsonResult.setMsg("添加成功");
            }else{
                throw  new BusinessException("系统开小差");
            }
        }else{//修改
            int update = archivesService.update(archives);
            if (update > 0){
                jsonResult.setCode("200");
                jsonResult.setMsg("修改成功");
            }else{
                throw  new BusinessException("系统开小差");
            }
        }
        return jsonResult.getValues();
    }

    /***
     * 用户详细信息分页列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getArchivesList")
    public Map<String,Object> getArchivesList(@RequestParam("page")Integer page ,@RequestParam("pageSize") Integer pageSize){
        JsonResult jsonResult = new JsonResult();
        PageHelper.startPage(page,pageSize);
        List<Archives> archivesList = archivesService.getArchivesList();
        PageInfo<Archives> archivesPageInfo = new PageInfo<>(archivesList);
        jsonResult.setCode("200");
        jsonResult.setData(archivesList);
        return  jsonResult.getValues();
    }

    /***
     * 根据id删除用户详细信息
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Map<String,Object> deleteById(@RequestParam("id") Integer id){
        JsonResult jsonResult = new JsonResult();
        int i = archivesService.deleteById(id);
        if(i > 0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else{
            throw  new BusinessException("系统开小差");
        }
        return jsonResult.getValues();
    }

    /***
     * 根据用户id删除用户详细信息
     * @param userId
     * @return
     */
    @GetMapping("/deleteByUserId")
    public Map<String,Object> deleteByUserId(@RequestParam("userId")Integer userId){
        JsonResult jsonResult = new JsonResult();
        int i = archivesService.deleteByyUserId(userId);
        if(i > 0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else{
            throw  new BusinessException("系统开小差");
        }
        return  jsonResult.getValues();
    }
}
